﻿@{
    ViewBag.Title = "CreateTrain";
    ViewBag.First = "车辆管理";
    ViewBag.Second = "创建驾驶员培训记录";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
    var list = ViewData["type"] as IList<Sc.Domain.BaseItem>;
}
<script src="~/Content/laydate-v1.1/laydate/laydate.js"></script>
<script type="text/javascript">
    $(document).ready(function (e) {
        $(".select2").uedSelect({
            width: 120
        });
        initSingleDate("txtTime", "YYYY/MM/DD");
    });
    //司机选择
    function selectDriver() {
        layer.open({
            type: 2,
            title: "驾驶员选择",
            shade: [0],
            area: [($(window).width() - 20) + "px", ($(window).height() - 80) + "px"],
            content: ['@Url.Action("SelectDriver", "CarInfo")', 'yes']
        });
    }
    //司机选择后
    function sureDriver(list) {
        var table = document.getElementById("listData");
        for (var i = 0; i < list.length; i++) {
            var row = table.insertRow(table.rows.length);
            var index = row.insertCell(0);
            index.innerHTML = (i + 1).toString();
            var name = row.insertCell(1);
            name.innerHTML = list[i].Name;
            var sc = row.insertCell(2);
            sc.innerHTML = '<input type="text" tag=' + list[i].Id + ' onkeyup="judgeNum(this)" style="border:none;border-bottom:solid 1px #ccc;margin-bottom:5px" class="scinput" />';
            var opa = row.insertCell(3);
            opa.innerHTML = '<a href="javascript:void(0)" onclick="del(this);" class="tablelink">删除</a>';
        }
        layer.closeAll();
    }
    //判断是否是数字
    function judgeNum(obj) {
        if (isNaN(obj.value)) {
            layer.tips("得分必须为数字", obj, {
                tips: [2, 'red'],
                time: 1000
            });
            obj.value = "";
        }
    }
    //删除
    function del(obj) {
        layer.confirm("您确定要删除吗？", function () {
            $(obj).parent().parent().remove();
            layer.closeAll();
        });
    }
    //数据保存
    function saveData() {
        var selType = $("#selType").val();
        var txtTime = $("#txtTime").val();
        var list = $("#listData tr").find("input[type=text]");
        if (selType == 0) {
            Tips("请选择培训类型", "selType");
            return false;
        }
        if (txtTime.length <= 0) {
            Tips("请选择培训时间", "txtTime");
            return false;
        }
        if (list.length <= 0) {
            loadTip("请至少选择一个驾驶员", 2, 1000);
            return false;
        }
        var arr = new Array();
        for (var i = 0; i < list.length; i++) {
            var ent = new Object();
            ent.Driver = $(list[i]).attr("tag");
            ent.Score = $(list[i]).val();
            arr.push(ent);
        }
        var url = "@Url.Action("SaveData")";
        var load = loadTip("数据提交中，请稍后", 16, 0);
        $.post(url, { selType: selType, txtTime: txtTime, data: JSON.stringify(arr) }, function (data) {
            if (data > 0) {
                layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () { back()});
            } else {
                loadTip("数据保存失败", 2, 1000);
            }
        });
    }
    function back() {
        window.location.href = "@Url.Action("index")";
    }
</script>
<div class="formbody">
    <div class="formtitle"><span>创建驾驶员培训记录</span></div>
    <div class="tools">
        <ul class="seachform">
            <li>
                <label>培训类型</label>
                <div class="vocation">
                    <select name="select" class="select2" id="selType">
                        <option value="0">----请选择----</option>
                        @foreach (var item in list)
                        {
                            <option value="@item.Id">@item.Name</option>
                        }
                    </select>
                </div>
            </li>
            <li>
                <label>培训时间</label>
                <input type="text" id="txtTime" readonly="readonly" class="scinput" />
            </li>
            <li>
                <label>&nbsp;</label>
                <input type="button" class="scbtn" onclick="selectDriver();" value="选择驾驶员" />
                <input type="button" class="scbtn" onclick="saveData();" value="保 存" />
                <input type="button" class="scbtn" onclick="back();;" value="返 回" />
            </li>
        </ul>
    </div>
    <table class="tablelist">
        <thead>
            <tr>
                <th width="60">编号</th>
                <th>姓名</th>
                <th>得分</th>
                <th width="60">操作</th>
            </tr>
        </thead>
        <tbody id="listData">
           
        </tbody>
    </table>
</div>